package servlet;

import bean.Cart;
import bean.Order;
import bean.OrderItem;
import com.sun.org.apache.xpath.internal.operations.Or;
import jdk.management.resource.internal.inst.SocketOutputStreamRMHooks;
import service.OrderService;
import service.impl.BookServiceImpl;
import service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class OrderServlet extends BookServlet {

    OrderServiceImpl orderService = new OrderServiceImpl();
    BookServiceImpl bookService = new BookServiceImpl();
    protected void OrderStatusUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderCode = (String)req.getSession().getAttribute("orderCode");
        int i = orderService.orderStatusUpdate(orderCode);
        if (i > 0) {
            resp.sendRedirect("orderServlet?action=showMyOrder");
        }else{
            System.out.println("更改失败");
        }


    }

        protected void showOrderDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String orderCode = req.getParameter("orderCode");
        long orderNumber = Long.parseLong(orderCode);
        List<OrderItem> orderItems = orderService.showOrderDetail(orderNumber);
        System.out.println(orderItems);
        req.getSession().setAttribute("orderCode",orderCode);
        req.getSession().setAttribute("orderItems",orderItems);
        req.getRequestDispatcher("/pages/order/orderItem.jsp").forward(req, resp);
    }

        protected void showMyOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Number number = (Number) session.getAttribute("userId");
            List<Order> order = orderService.showMyOrders(number.intValue());
            session.setAttribute("order",order);
            req.getRequestDispatcher("/pages/order/order.jsp").forward(req, resp);
//            orderService.showOrderDetail();

        }
    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session = req.getSession();

        //获取购物车信息
        Cart cart = (Cart)session.getAttribute("cartInfo");

        //获取后，将session域中的购物车信息删除
        session.removeAttribute("carInfo");

        //获取登录的用户id
        Number number = (Number) session.getAttribute("userId");


        //更新库存和销量
        int[] result = bookService.syncStockAndSales(cart.getItems());

        if( result != null){
            req.getSession().setAttribute("stockOut",result);
            resp.sendRedirect(req.getContextPath()+"/cartServlet?action=getCartInfo");
            return;
        }

        //生成订单和订单详情
        String orderCode = orderService.createOrder(cart, number.intValue());

        session.setAttribute("orderCode",orderCode);

//        Cookie cookie = new Cookie("orderCode",orderCode);
//        cookie.setMaxAge(3);
//        resp.addCookie(cookie);

        resp.sendRedirect(req.getContextPath() + "/pages/cart/checkout.jsp");


    }
}
